﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using AutoMapper;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using Sautom.ClientComunication;
using Sautom.ClientComunication.CommandService;
using Sautom.ClientComunication.QueriesService;
using Sautom.ClientComunication.Services;
using Sautom.Modules.Client.Controls.ViewModels;
using Sautom.Modules.Client.Models;

namespace Sautom.Modules.Client.ViewModels 
{
    public sealed class EditOrderAdvancedViewModel : NavigationAwareNotificationObject
    {
        #region Constructor
        public EditOrderAdvancedViewModel(IRegionManager regionManager, ServiceFactory serviceFactory)
        {
            RegionManager = regionManager;

            QueriesServiceClient = serviceFactory.GetQueriesService();
            CommandServiceClient = serviceFactory.GetCommandsService();

            BackCommand = new DelegateCommand(ExecuteBackCommand);

            SaveCommand = new DelegateCommand(ExecuteSaveCommand, () => OrderEditorViewModel.Intensity != null && OrderEditorViewModel.HouseType != null);
        }

        #endregion

        #region Commands
        public DelegateCommand BackCommand { get; private set; }
        public DelegateCommand SaveCommand { get; private set; }

        private void ExecuteBackCommand()
        {
            RegionManager.RequestNavigate(RegionProvider.MainRegion,
                                          new Uri(PathProvider.ClientDetails + new UriQuery { { "clientId", this.ClientId.ToString() } }, UriKind.Relative));
        }

        private void ExecuteSaveCommand()
        {
            Mapper.CreateMap<OrderEditorViewModel, OrderEditDtoInput>()
                .ForMember(d => d.IntensityId, o => o.ResolveUsing(v => v.Intensity.Id))
                .ForMember(d => d.HouseTypeId, o => o.ResolveUsing(v => v.HouseType.Id))
                .ForMember(d => d.ManagerId, o => o.ResolveUsing(v => v.SelectedManager.Id))
                .ForMember(d => d.ProposalId, o => o.ResolveUsing(v => v.SelectedProposal.Id));
            var orderData = Mapper.Map<OrderEditDtoInput>(this.OrderEditorViewModel);

            //CommandServiceClient.UpdateOrderForClient(ClientId, orderData);

            ExecuteBackCommand();
        }
        #endregion

        #region Properties
        private IRegionManager RegionManager { get; set; }
        private IQueriesService QueriesServiceClient { get; set; }
        private ICommandService CommandServiceClient { get; set; }
        #endregion

        #region ViewModel Properties

        public Guid ClientId { get; set; }
        public Guid OrderId { get; set; }
        
        public OrderEditorViewModel OrderEditorViewModel { get; private set; }
        #endregion

        #region INavigationAware

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);

            // Initial load - Load based on ID passed in
            string id = navigationContext.Parameters["clientId"];
            string orderId = navigationContext.Parameters["orderId"];
            if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(orderId)) return;

            ClientId = Guid.Parse(id);
            OrderId = Guid.Parse(orderId);

            CreateOrderInfoDtoOutput data = QueriesServiceClient.GetOrderCreationData();
            OrderEditorViewModel = new OrderEditorViewModel(SaveCommand, data.Proposals, data.ResponsibleManagers);
        }

        #endregion
    }
}
